package tw.com.hitevision.whiteboard.android.widgets;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import androidx.constraintlayout.widget.ConstraintLayout;

import java.util.ArrayList;

import tw.com.hitevision.whiteboard.android.R;
import tw.com.hitevision.whiteboard.android.options.OptionsRecyclerViewAdapter;

public class PluginsToolbarMenuView extends ConstraintLayout {
    public static final String TAG = PluginsToolbarMenuView.class.getSimpleName();
    public static final String VISUALIZER_PACKAGE_NAME = "com.newline.visualizer";
    public static final String VISUALIZER_CLASS_NAME = "com.newline.visualizer.MainActivity";
    public static final String SCREEN_RECORDER_PACKAGE_NAME = "tw.com.newline.screenrecoder.android";
    public static final String SCREEN_RECORDER_CLASS_NAME = "tw.com.newline.screenrecoder.android.MainActivity";
    public static final String SPLIT_MODE_PACKAGE_NAME = "tw.com.newline.splitmenu";
    public static final String SPLIT_MODE_CLASS_NAME = "tw.com.newline.splitmenu.SplitMenuActivity";
    private PluginsListener listener;
    private ConstraintLayout visualizer;
    private ConstraintLayout recorder;
    private ConstraintLayout splitMode;
    private ConstraintLayout layoutRulers;
    private OptionsRulerMenuView optionsRulerMenuView;
    private Context context;
    private OptionsRecyclerViewAdapter.OptionsRecyclerViewItemListener optionsRecyclerViewItemListener;
    private OnTouchListener onToggleInputListener;

    public PluginsToolbarMenuView(Context context) {
        super(context);
        init(context);
    }

    public PluginsToolbarMenuView(Context context, AttributeSet attr) {
        super(context, attr);
        init(context);
    }

    public PluginsToolbarMenuView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        inflate(context, R.layout.menu_plugins_toolbar, this);
        this.context = context;

        visualizer = findViewById(R.id.layout_visualizer);
        visualizer.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                setVisibility(GONE);
                if (listener != null) {
                    listener.onClickVisualizer(view);
                }
            }
        });

        recorder = findViewById(R.id.layout_recorder);
        recorder.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                setVisibility(GONE);
                if (listener != null) {
                    listener.onClickScreenRecorder(view);
                }
            }
        });

        splitMode = findViewById(R.id.layout_splitmode);
        splitMode.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                setVisibility(GONE);
                if (listener != null) {
                    listener.onClickSplitMode(view);
                }
            }
        });

        layoutRulers = findViewById(R.id.layout_rulers);
        layoutRulers.setOnClickListener(view -> {
            setVisibility(GONE);
            if (optionsRecyclerViewItemListener != null) {
                optionsRecyclerViewItemListener.OnItemClick(context.getString(R.string.plugin_app_rulers));
            }
        });

        checkPluginsExit();
        //In order to avoid the touch event penetrating
        this.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return true;
            }
        });
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (visibility == VISIBLE) {
            checkPluginsExit();
        }
    }

    private void checkPluginsExit() {
        if (visualizer != null) {
            visualizer.setVisibility(isAppInstalled(VISUALIZER_PACKAGE_NAME) ? VISIBLE : GONE);
        }

        if (recorder != null) {
            recorder.setVisibility(isAppInstalled(SCREEN_RECORDER_PACKAGE_NAME) ? VISIBLE : GONE);
        }

        if (splitMode != null) {
            splitMode.setVisibility(isAppInstalled(SPLIT_MODE_PACKAGE_NAME) ? VISIBLE : GONE);
        }

        if (optionsRulerMenuView != null) {
            optionsRulerMenuView.setVisibility(GONE);
        }
    }

    public void setPluginsListener(PluginsListener listener) {
        this.listener = listener;
    }

    public interface PluginsListener {
        void onClickVisualizer(View v);

        void onClickScreenRecorder(View v);

        void onClickSplitMode(View v);
    }

    private boolean isAppInstalled(String packagename) {
        PackageInfo packageInfo;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(packagename, 0);
        } catch (PackageManager.NameNotFoundException e) {
            packageInfo = null;
            e.printStackTrace();
        }
        if (packageInfo == null) {
            return false;
        } else {
            return true;
        }
    }

    public void updateOptionsRulerIconSelected(ArrayList<Boolean> selectedList) {
        if (optionsRulerMenuView != null) {
            optionsRulerMenuView.setSelectedIconList(selectedList);
        }
    }

    public void setInit(OptionsRecyclerViewAdapter.OptionsRecyclerViewItemListener listener, OnTouchListener onToggleInputListener) {
        this.optionsRecyclerViewItemListener = listener;
        this.onToggleInputListener = onToggleInputListener;
    }
}